#pragma once

#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t* lock)
        : _lock(lock)
    {}
    void lock()
    {
        if(_lock) pthread_mutex_lock(_lock);
    }
    void unlock()
    {
        if(_lock) pthread_mutex_unlock(_lock);
    }
    ~Mutex()
    {}
private:
    pthread_mutex_t* _lock;
};

class LockGuard
{
public:
    // 对象构造时自动加锁，析构时自动解锁
    LockGuard(pthread_mutex_t* mutex)// 这里有一个参数是因为，锁是什么样的，是由使用LockGuard的用户决定的，类似于智能指针
        : _mutex(mutex)
    {
        _mutex.lock();// 构造函数中加锁
    }
    ~LockGuard()
    {
        _mutex.unlock();// 析构函数中解锁
    }

private:
    Mutex _mutex;
};